<template>
  <div class="app-container">
    <!-- 表单 -->
    <el-form
      :inline="true"
      :model="formInline"
      ref="formInline"
      class="demo-form-inline"
      size="mini"
    >
      <el-form-item label="标签名称" prop="name">
        <el-input
          v-model="formInline.name"
          placeholder="请输入标签名称"
        ></el-input>
      </el-form-item>
      <el-form-item label="分类名称" prop="categoryName">
        <el-select
          v-model="formInline.categoryName"
          placeholder="请选择"
          style="width:90px"
        >
          <el-option
            :label="item.name"
            :value="item.id"
            v-for="item in categoryList"
            :key="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="findList" icon="el-icon-search"
          >查询</el-button
        >
        <el-button @click="resetForm" icon="el-icon-refresh">重置</el-button>
        <el-button
          type="primary"
          @click="addItem"
          icon="el-icon-circle-plus-outline"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 表格 -->
    <el-table :data="tableData" stripe border style="width: 100%">
      <el-table-column type="index" label="序号" width="60" align="center">
      </el-table-column>
      <el-table-column prop="name" label="标签名称" align="center">
      </el-table-column>
      <el-table-column prop="categoryName" label="分类名称" align="center">
      </el-table-column>
      <el-table-column prop="address" label="操作" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="editItem(scope.row.id)"
            >编辑</el-button
          >
          <el-button size="mini" type="danger" @click="delItem(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 新增和编辑 弹出框 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="dialogVisible"
      width="30%"
      @close="close"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item label="标签名称:" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="分类名称" prop="categoryId">
          <el-select
            v-model="ruleForm.categoryId"
            placeholder="请选择"
            style="width:100%"
          >
            <el-option
              :label="item.name"
              :value="item.id"
              v-for="item in categoryList"
              :key="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 分页 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="current"
      :page-sizes="[5, 10, 20, 50]"
      :page-size="size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import api from "@/api/label";
let that;

export default {
  data() {
    return {
      formInline: {
        name: "",
        categoryName: ""
      },
      tableData: [],
      categoryList: [],
      current: 1,
      size: 20,
      total: 0,
      dialogTitle: "",
      dialogVisible: false,
      ruleForm: {
        name: "",
        categoryId: ""
      },
      rules: {
        name: [{ required: true, message: "标签名不能为空", trigger: "blur" }],
        categoryId: [
          { required: true, message: "分类名不能为空", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    // 获取列表数据
    getList() {
      api.getList(this.formInline, this.current, this.size).then(response => {
        if (response.code == 20000) {
          this.tableData = response.data.records;
          this.total = response.data.total;
        }
      });
    },
    // 获取分类名称列表
    getCategoryList() {
      api.getCategoryList().then(response => {
        if (response.code == 20000) {
          this.categoryList = response.data;
        }
      });
    },
    // 分页
    handleSizeChange(val) {
      this.size = val;
      this.getList();
    },
    handleCurrentChange(val) {
      this.current = val;
      this.getList();
    },
    // 查询
    findList() {
      this.getList();
    },
    // 重置
    resetForm() {
      this.$refs.formInline.resetFields();
      this.getList();
    },
    // 新增
    addItem() {
      this.dialogVisible = true;
      this.dialogTitle = "新增";
    },
    // 编辑
    editItem(id) {
      api.findItem(id).then(response => {
        this.dialogVisible = true;
        this.dialogTitle = "编辑";
        if (response.code == 20000) {
          this.ruleForm = response.data;
        }
      });
    },
    // 新增 编辑 提交
    submit() {
      this.$refs.ruleForm.validate(valid => {
        if (valid) {
          if (this.dialogTitle == "新增") {
            api.addItem(this.ruleForm).then(response => {
              if (response.code == 20000) {
                console.log(response);
                console.log(this.ruleForm);
                this.dialogVisible = false;
                this.$message.success(response.message);
                this.getList();
              }
            });
          } else {
            api.editItem(this.ruleForm).then(response => {
              if (response.code == 20000) {
                this.dialogVisible = false;
                this.$message.success(response.message);
                this.getList();
              }
            });
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    // 关闭弹出框
    close() {
      this.$refs.ruleForm.resetFields();
    },
    // 删除
    delItem(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          api.delItem(id).then(response => {
            if (response.code == 20000) {
              this.$message.success(response.message);
              this.getList();
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除"
          });
        });
    }
  },
  created() {
    this.getList();
    this.getCategoryList();
    // 为过滤器存储   this
    that = this;
  }
};
</script>
